(function ($) {
    var thisObj;
    $.fn.zhhQQ = function (options) {
        if ($.type(options) == "function") {
            $.fn.zhhQQ.defaults.clickFn = options;
            var opts = $.fn.zhhQQ.defaults;
        } else {
            var opts = $.extend({}, $.fn.zhhQQ.defaults, options);
        }
        thisObj = this;
        init(opts);
    }

    function qqTitleList() {
        var qqTitle = "微笑,撇嘴,色,发呆,得意,流泪,害羞,闭嘴,睡,大哭,尴尬,发怒,调皮,呲牙,惊讶,难过,酷,冷汗,折磨,吐,偷笑,可爱,白眼,傲慢,饥饿,困,惊恐,流汗,憨笑,大兵,奋斗,咒骂,疑问,嘘,晕,抓狂,衰,骷髅,敲打,再见,擦汗,抠鼻,鼓掌,糗大了,坏笑,左哼哼,右哼哼,哈欠,鄙视,委屈,快哭了,阴险,亲亲,吓,可怜,菜刀,西瓜,啤酒,篮球,乒乓,咖啡,饭,猪头,玫瑰,凋谢,嘴唇,爱心,心碎,蛋糕,闪电,炸弹,刀,足球,瓢虫,便便,月亮,太阳,礼物,拥抱,强,弱,握手,胜利,抱拳,勾引,拳头,差劲,爱你,NO,OK,爱情,飞吻,跳跳,发抖,怄火,转圈,磕头,回头,跳绳,挥手,激动,街舞,献吻,左太极,右太极";
        return qqTitle.split(",");
    }

    function init(opts) {
        var qqTitleArray = qqTitleList()

        var qqHtmlBox = $("<div>", {"class": "zhh-qq-face-box"});
        var qqHtmlInner = $("<div>", {"class": "zhh-qq-face-inner"});
        qqHtmlInner.appendTo(qqHtmlBox);
        $.each(qqTitleArray, function (index, item) {
            $("<a>", {"title": item, "href": "javascript:;", "data-index": index + 1}).appendTo(qqHtmlInner);
        });
        $("body").append(qqHtmlBox);

        //位置
        var thisOffset = thisObj.offset();
        switch (opts.location) {
            case "bottom":
                qqHtmlBox.css({top: thisOffset.top + thisObj.outerHeight(), left: thisOffset.left});
                break;
            case "top":
                qqHtmlBox.css({bottom: thisOffset.top + qqHtmlBox.height(), left: thisOffset.left});
                break;
            case "left":
                qqHtmlBox.css({bottom: thisOffset.top, left: thisOffset.left - qqHtmlBox.width() - thisObj.width()});
                break;
            case "right":
                qqHtmlBox.css({bottom: thisOffset.top, left: thisOffset.left + thisObj.width()});
                break;
        }
        qqHtmlInner.find("a").click(function (event) {
            if ($.type($.fn.zhhQQ.defaults.clickFn) == "function") {
                var title = $(this).attr("title");
                $.fn.zhhQQ.defaults.clickFn(title);
            }
            event.stopPropagation();
            event.preventDefault();
            qqHtmlBox.remove();
        });

        $("body").one("click", function () {
            qqHtmlBox.remove();
        });
    }

    $.zhhQQGif = function (qqTitle) {
        var qqTitleArray = qqTitleList()
        var qqIndex = 0;
        $.each(qqTitleArray, function (index, item) {
            if (item == qqTitle) {
                qqIndex = index + 1;
                return false;
            }
        });
        return qqIndex;
    }

    // 插件的defaults
    $.fn.zhhQQ.defaults = {
        location: 'bottom',
        clickFn: null,
    };
})(jQuery)